/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.company.graphics.builders;

import java.awt.Color;
import java.util.List;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author admin
 */
public class JFreeChartBuilder {
    
    private XYSeriesCollection dataSeries;
    private double maxRange;
    
    public JFreeChartBuilder(List<XYSeries> seriesList, double maxRange) {
        this.dataSeries = new XYSeriesCollection();
        for (XYSeries series : seriesList) {
            dataSeries.addSeries(series);
        }
        this.maxRange = maxRange*1.15;
    }
    
    public JFreeChart buildGraphics() {
        JFreeChart chart = ChartFactory
                .createXYLineChart("График", "x", "y",
                        dataSeries,
                        PlotOrientation.VERTICAL,
                        true, true, true);
        chart.setBackgroundPaint(Color.WHITE);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainGridlinePaint(Color.GRAY);
        plot.setRangeGridlinePaint(Color.GRAY);
        plot.getDomainAxis().setAutoRange(false);
        plot.getDomainAxis().setRange(-maxRange, maxRange);
        plot.getRangeAxis().setRange(-maxRange, maxRange);
        plot.getRangeAxis().setAutoRange(false);
        Random r = new Random();
        for (int i = 0; i < dataSeries.getSeriesCount(); i++) {
            plot.getRenderer().setSeriesPaint(i, new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        }
        
        return chart;
    }
    
}
